Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@casual-simulation/rate-limit-redis

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@casual-simulation/rate-limit-redis

A Redis store for the `express-rate-limit` middleware

  • 3.3.13
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
decreased by-43.48%
Maintainers
0
Weekly downloads
 
Created
Source

rate-limit-redis

Github Workflow Status npm version GitHub Stars npm downloads

A redis store for the express-rate-limit middleware.

Installation

From the npm registry:

# Using npm
> npm install rate-limit-redis
# Using yarn or pnpm
> yarn/pnpm add rate-limit-redis

From Github Releases:

# Using npm
> npm install https://github.com/wyattjoh/rate-limit-redis/releases/download/v{version}/rate-limit-redis.tgz
# Using yarn or pnpm
> yarn/pnpm add https://github.com/wyattjoh/rate-limit-redis/releases/download/v{version}/rate-limit-redis.tgz

Replace {version} with the version of the package that you want to your, e.g.: 3.0.0.

Usage

Importing

This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects.

This package requires you to use Node 14 or above.

Import it in a CommonJS project (type: commonjs or no type field in package.json) as follows:

const RedisStore = require('rate-limit-redis');

Import it in a ESM project (type: module in package.json) as follows:

import RedisStore from 'rate-limit-redis';

Examples

To use it with a node-redis client:

import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import { createClient } from 'redis';

// Create a `node-redis` client
const client = createClient({
    // ... (see https://github.com/redis/node-redis/blob/master/docs/client-configuration.md)
});
// Then connect to the Redis server
await client.connect();

// Create and use the rate limiter
const limiter = rateLimit({
    // Rate limiter configuration
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
    standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
    legacyHeaders: false, // Disable the `X-RateLimit-*` headers

    // Redis store configuration
    store: new RedisStore({
        sendCommand: (...args: string[]) => client.sendCommand(args),
    }),
});
app.use(limiter);

To use it with a ioredis client:

import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import RedisClient from 'ioredis';

// Create a `ioredis` client
const client = new RedisClient();
// ... (see https://github.com/luin/ioredis#connect-to-redis)

// Create and use the rate limiter
const limiter = rateLimit({
    // Rate limiter configuration
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
    standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
    legacyHeaders: false, // Disable the `X-RateLimit-*` headers

    // Redis store configuration
    store: new RedisStore({
        // @ts-expect-error - Known issue: the `call` function is not present in @types/ioredis
        sendCommand: (...args: string[]) => client.call(...args),
    }),
});
app.use(limiter);

Configuration

sendCommand

The function used to send commands to Redis. The function signature is as follows:

(...args: string[]) => Promise<number> | number

The raw command sending function varies from library to library; some are given below:

LibraryFunction
node-redisasync (...args: string[]) => client.sendCommand(args)
ioredisasync (...args: string[]) => client.call(...args)
handy-redisasync (...args: string[]) => client.nodeRedis.sendCommand(args)
tedisasync (...args: string[]) => client.command(...args)
redis-fast-driverasync (...args: string[]) => client.rawCallAsync(args)
yoredisasync (...args: string[]) => (await client.callMany([args]))[0]
noderisasync (...args: string[]) => client.callRedis(...args)
prefix

The text to prepend to the key in Redis.

Defaults to rl:.

resetExpiryOnChange

Whether to reset the expiry for a particular key whenever its hit count changes.

Defaults to false.

License

MIT © Wyatt Johnson

FAQs

Package last updated on 08 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc